import { message, Modal } from "antd";
import Api from "./service";

//订单状态更改配置文件
const operaConfig = {
	//审核订单
	'vertifyOrder': {
		operaApi: Api.vertifyOrder,
		formatParams(item) {
			const params = {
				orderIds: item.selectKeys
			};
			return params;
		}
	},
	//返审核订单
	'reVertifyOrder': {
		operaApi: Api.reVertifyOrder,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds,
				tradeNo: item.tradeNo
			};
			return params;
		}
	},
	//作废订单
	'voidOrder': {
		operaApi: Api.voidOrder,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds,
				tradeNo: item.tradeNo,
				cancelMsg: item.cancelMsg,
				reason: item.reason
			};
			return params;
		}
	},
	//取消作废订单
	'cancelVoidOrder': {
		operaApi: Api.cancelVoidOrder,
		formatParams(item) {
			return item.orderIds;
		}
	},

	//转异常订单
	'abnormalOrder': {
		operaApi: Api.abNormalOrder,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds,
				description: item.description,
				errorId: item.errorId
			};
			return params;
		}
	},

	//转正常订单
	'normalOrder': {
		operaApi: Api.normalOrder,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds,
				tradeNo: item.tradeNo,
				refundOrderStrategy: item.refundOrderStrategy
			};
			return params;
		}
	},

	//物流回传
	'logisticReturn': {
		operaApi: Api.logisticReturn,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds
			};
			return params;
		}
	},

	//智能合单
	'autoMergeOrder': {
		operaApi: Api.autoMergeOrder,
		formatParams(item) {
			const params = {
				waitAutoMergeOrderIds: item.waitAutoMergeOrderIds
			};
			return params;
		}
	},

	//按策略拆单
	'autoSplitOrder': {
		operaApi: Api.strategySplitOrder,
		formatParams(item) {
			const params = {
				orderIds: item.orderIds,
				strategyId: item.strategyId
			};
			return params;
		}
	},
};

//操作接口
const updateOrder = async (operaApi, params, callBack) => {
	const { success, data } = await operaApi(params);
	//loader.hide(loading);
	if (success) {
		if (data?.msg) {
			Modal.info({
				centered: true,
				title: "操作结果提示",
				content: (
					<div>
						<p>{data.msg}</p>
					</div>
				),
				onOk() {
					callBack && callBack(data);
				}
			});
		} else {
			callBack && callBack(data);
			message.success("操作成功！");
		}
	} else {
		callBack && callBack();
	}
};

//更改订单状态
export const updateOrderStatus = async (scene, item, callBack) => {
	const { operaApi, formatParams } = operaConfig[scene];
	//参数设置
	const params = formatParams(item);
	updateOrder(operaApi, params, callBack);
};
